using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WIT.DAQ.Core.Forms.UserControls;

/// <summary>
///
/// </summary>
internal class UC_DAQItemParams_For_ModbusRtu : UserControl
{
	/// <summary>
	/// 地址
	/// </summary>
	internal string ADDRESS = string.Empty;

	/// <summary>
	/// 长度
	/// </summary>
	internal int? LENGTH = null;

	/// <summary> 
	/// 必需的设计器变量。
	/// </summary>
	private IContainer components = null;

	private Label label1;

	private TextBox txtAddress;

	private Label label2;

	private Label label3;

	private Label lblNotice;

	private NumericUpDown nudLength;

	private Panel pnlStringParams;

	/// <summary>
	///
	/// </summary>
	internal UC_DAQItemParams_For_ModbusRtu()
	{
		InitializeComponent();
	}

	/// <summary>
	///
	/// </summary>
	/// <param name="address"></param>
	/// <param name="length"></param>
	internal UC_DAQItemParams_For_ModbusRtu(string address, int? length)
	{
		InitializeComponent();
		ADDRESS = address;
		LENGTH = length;
		txtAddress.Text = ADDRESS;
		nudLength.Value = ((!LENGTH.HasValue) ? 10 : LENGTH.Value);
	}

	/// <summary>
	/// 校验输入的内容是否合法
	/// </summary>
	/// <returns></returns>
	internal bool ValidateInputs(out string msg)
	{
		msg = string.Empty;
		bool flag = false;
		try
		{
			ADDRESS = txtAddress.Text.Trim();
			if (string.IsNullOrEmpty(ADDRESS) || !int.TryParse(ADDRESS, out var _))
			{
				throw new Exception("请输入正确的地址");
			}
			if (pnlStringParams.Visible)
			{
				LENGTH = Convert.ToInt32(nudLength.Value);
			}
			else
			{
				LENGTH = null;
			}
			flag = true;
		}
		catch (Exception ex)
		{
			flag = false;
			msg = ex.Message;
		}
		return flag;
	}

	/// <summary> 
	/// 清理所有正在使用的资源。
	/// </summary>
	/// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary> 
	/// 设计器支持所需的方法 - 不要修改
	/// 使用代码编辑器修改此方法的内容。
	/// </summary>
	private void InitializeComponent()
	{
		this.label1 = new System.Windows.Forms.Label();
		this.txtAddress = new System.Windows.Forms.TextBox();
		this.label2 = new System.Windows.Forms.Label();
		this.label3 = new System.Windows.Forms.Label();
		this.lblNotice = new System.Windows.Forms.Label();
		this.nudLength = new System.Windows.Forms.NumericUpDown();
		this.pnlStringParams = new System.Windows.Forms.Panel();
		((System.ComponentModel.ISupportInitialize)this.nudLength).BeginInit();
		this.pnlStringParams.SuspendLayout();
		base.SuspendLayout();
		this.label1.AutoSize = true;
		this.label1.ForeColor = System.Drawing.Color.Red;
		this.label1.Location = new System.Drawing.Point(238, 38);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(17, 12);
		this.label1.TabIndex = 30;
		this.label1.Text = "* ";
		this.txtAddress.Location = new System.Drawing.Point(111, 35);
		this.txtAddress.Name = "txtAddress";
		this.txtAddress.Size = new System.Drawing.Size(121, 21);
		this.txtAddress.TabIndex = 0;
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(52, 38);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(53, 12);
		this.label2.TabIndex = 29;
		this.label2.Text = "Address:";
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(19, 2);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(53, 12);
		this.label3.TabIndex = 31;
		this.label3.Text = "长  度：";
		this.lblNotice.AutoSize = true;
		this.lblNotice.Font = new System.Drawing.Font("宋体", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		this.lblNotice.ForeColor = System.Drawing.SystemColors.ControlDark;
		this.lblNotice.Location = new System.Drawing.Point(205, 2);
		this.lblNotice.Name = "lblNotice";
		this.lblNotice.Size = new System.Drawing.Size(101, 12);
		this.lblNotice.TabIndex = 33;
		this.lblNotice.Text = "读取String时使用";
		this.nudLength.Location = new System.Drawing.Point(78, 0);
		this.nudLength.Maximum = new decimal(new int[4] { 1000, 0, 0, 0 });
		this.nudLength.Name = "nudLength";
		this.nudLength.Size = new System.Drawing.Size(121, 21);
		this.nudLength.TabIndex = 0;
		this.nudLength.Value = new decimal(new int[4] { 10, 0, 0, 0 });
		this.pnlStringParams.Controls.Add(this.nudLength);
		this.pnlStringParams.Controls.Add(this.lblNotice);
		this.pnlStringParams.Controls.Add(this.label3);
		this.pnlStringParams.Location = new System.Drawing.Point(33, 62);
		this.pnlStringParams.Name = "pnlStringParams";
		this.pnlStringParams.Size = new System.Drawing.Size(355, 23);
		this.pnlStringParams.TabIndex = 1;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.pnlStringParams);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.txtAddress);
		base.Controls.Add(this.label2);
		base.Name = "UC_DAQItemParams_For_Modbus";
		base.Size = new System.Drawing.Size(523, 163);
		((System.ComponentModel.ISupportInitialize)this.nudLength).EndInit();
		this.pnlStringParams.ResumeLayout(false);
		this.pnlStringParams.PerformLayout();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
